<?php
$hostname = "localhost";
$username = "rt_user";
$password = "rtpassword";
$strFlg = "";
$database = "rt_widgets";
$dbConnection = mysql_connect($hostname,$username,$password,$strFlg) or die('Can\'t connect'); 
mysql_select_db($database, $dbConnection) or die('can\'t connect to db');
if($_GET['type']=='video'){
    $query = mysql_query('SELECT * FROM tbl_video order by rank limit 10 ' ,$dbConnection) or die(mysql_error());
    $incr=0;
    $record = "";
    $arrRecords = array();
    while($record = mysql_fetch_object($query) ){
      $arrRecords[$incr] = $record;
      $incr++;
    }
    $arrdatavideo = array();
    for($i=0;$i<count($arrRecords);$i++){
        $arrdatavideo[$i]->video = $arrRecords[$i]->link;
        $arrdatavideo[$i]->rank = $arrRecords[$i]->rank;
        $arrdatavideo[$i]->desc = $arrRecords[$i]->description;
    }
    $arrdata = $arrdatavideo;
    echo(json_encode($arrdata));
}
if($_GET['type']=='photo'){
    $query_photo = mysql_query('SELECT * FROM tbl_photos order by rank limit 10 ' ,$dbConnection) or die(mysql_error());
    $incr=0;
    $record = "";
    $arrRecords = array();
    while($record = mysql_fetch_object($query_photo) ){
      $arrRecords[$incr] = $record;
      $incr++;
    }
    $arrdataphoto = array();
    for($i=0;$i<count($arrRecords);$i++){
        $arrdataphoto[$i]->photo = $arrRecords[$i]->link;
        $arrdataphoto[$i]->rank = $arrRecords[$i]->rank;
        $arrdataphoto[$i]->desc = $arrRecords[$i]->description;
    }
    $arrdata = $arrdataphoto;
    echo(json_encode($arrdata));
}

if($_GET['type']=='galary' ){
    $user_id = $_GET['user_id'];
    $client = $_GET['client'];
    $query = "SELECT * FROM join_user_client j left join tbl_clients c on c.id= j.client_id where j.user_key_id='".$user_id."' and c.name='".$client."'";
    $query_user = "";
    $query_user = mysql_query($query,$dbConnection) or die(mysql_error()."mysql error occured in galary");
    $record = "";
    $record = mysql_fetch_object($query_user);
    
    if(1){
        $user_id = 1;
        //$record->user_id;!empty($record)
        $status = 'true';
        $query_video = mysql_query('SELECT * FROM tbl_video v left join join_user_video uv on uv.video_id=v.id  where uv.user_id = "'.$user_id.'"  limit 10 ' ,$dbConnection) or die(mysql_error());
        $incr=0;
        $recorddata = "";
        $arrRecords = array();
        while($recorddata = mysql_fetch_object($query_video) ){
          $arrRecords[$incr] = $record;
          $incr++;
        }
        $arrdatavideo = array();
        for($i=0;$i<count($arrRecords);$i++){
            $arrdatavideo[$i]->video = $arrRecords[$i]->link;
            $arrdatavideo[$i]->rank = $arrRecords[$i]->rank;
            $arrdatavideo[$i]->desc = $arrRecords[$i]->description;
        }
        $query_photo = mysql_query('SELECT * FROM tbl_photos v left join join_user_photo  uv on uv.photo_id=v.id  where uv.user_id = "'.$user_id.'" order by uv.rank limit 10 ' ,$dbConnection) or die(mysql_error());
        $incr=0;
        $arrRecordsPhotoData = array();
        $arrRecordsPhoto = array();
        while($arrRecordsPhoto = mysql_fetch_object($query_photo) ){
          $arrRecordsPhotoData[$incr] = $arrRecordsPhoto;
          $incr++;
        }
        $arrdataphoto = array();
        for($i=0;$i<count($arrRecordsPhotoData);$i++){
            $arrdataphoto[$i]->photo = $arrRecordsPhotoData[$i]->link;
            $arrdataphoto[$i]->rank = $arrRecordsPhotoData[$i]->rank;
            $arrdataphoto[$i]->desc = $arrRecordsPhotoData[$i]->description;
        }
        $arrdata = $arrdataphoto;
    } else {
        $status = 'false';
        $arrdatavideo = array();
        $arrdataphoto = array();
    }

    echo(json_encode(array('status'=>$status,'photodata'=>$arrdataphoto,'videodata'=>$arrdatavideo)));
}


?>
